package model

import (
	// "fmt"
	"time"

	"lab.com/conf"
)

// Cattle return cattle struct
type Achievement struct {
	ID         int       `json:"id" gorm:"column:id; PRIMARY_KEY; AUTO_INCREMENT"`
	Title      string    `json:"title" gorm:"column:title;type:varchar(50);default:''"`
	Abstract   string    `json:"abstract" gorm:"column:abstract;type:varchar(100);default:''"`
	Content    string    `json:"content" gorm:"column:content;type:text; not null"`
	Type       int       `json:"type" gorm:"column:type;type:tinyint(4);default:0"`
	IsShow     int       `json:"is_show" gorm:"column:is_show;type:tinyint(4);default:0"`
	IsHome     int       `json:"is_home" gorm:"column:is_home;type:tinyint(4);default:0"`
	Weight     int       `json:"weight" gorm:"column:weight;type:int(11);default:0"`
	CreateTime time.Time `json:"create_time" gorm:"column:create_time; type:int(11); not null"`
	UpdateTime time.Time `json:"update_time" gorm:"column:update_time; type:int(11); not null"`
}

func (Achievement) TableName() string {
	return "achievement"
}

// GlobalCreate 创建记录
func (ach *Achievement) GlobalCreate() error {
	db := conf.LabMysql.Model(&Achievement{}).Create(ach)
	if db.Error != nil {
		return db.Error
	}
	return nil
}

// GlobalUpdate 修改记录
func (ach *Achievement) GlobalUpdate(data map[string]interface{}, query interface{}, args ...interface{}) (int64, error) {
	db := conf.LabMysql.Model(&Achievement{}).Where(query, args...).Updates(data)
	if db.Error != nil {
		return 0, db.Error
	}
	return db.RowsAffected, nil
}

// GlobalFind 查找单条
func (ach *Achievement) GlobalFind(fields interface{}, query interface{}, args ...interface{}) error {
	db := conf.LabMysql.Model(&Achievement{}).Select(fields).Where(query, args...).Find(&ach)
	if db.Error != nil {
		return db.Error
	}
	return nil
}

// GlobalFindAll 查找多条
func (ach *Achievement) GlobalFindAll(fields interface{}, query interface{}, args ...interface{}) ([]Achievement, error) {
	var info []Achievement
	db := conf.LabMysql.Model(&Achievement{}).Select(fields).Where(query, args...).Find(&info)
	if db.Error != nil {
		return info, db.Error
	}
	return info, nil
}

// GlobalDelete 删除记录
func (ach *Achievement) GlobalDelete(query interface{}, args ...interface{}) error {
	db := conf.LabMysql.Model(&Achievement{}).Where(query, args...).Delete(&ach)
	if db.Error != nil {
		return db.Error
	}
	return nil
}

// GlobalFindIDS 获取IDS
func (ach *Achievement) GlobalFindIDS(query interface{}, args ...interface{}) ([]int, error) {
	var res []int
	db := conf.LabMysql.Model(&Achievement{}).Select("id").Where(query, args...).Find(&res)
	if db.Error != nil {
		return res, db.Error
	}
	return res, nil
}

// GlobalCount 统计数据
func (ach *Achievement) GlobalCount(field string, query interface{}, args ...interface{}) (int64, error) {
	var res int64
	db := conf.LabMysql.Model(&Achievement{}).Select(field).Where(query, args...).Count(&res)
	if db.Error != nil {
		return res, db.Error
	}
	return res, nil
}

// GlobalFindPage 分页查找
func (ach *Achievement) GlobalFindPage(fields interface{}, order interface{}, offset, limit int, query interface{}, args ...interface{}) ([]Achievement, error) {
	var info []Achievement
	db := conf.LabMysql.Model(&Achievement{}).Select(fields).Where(query, args...).Order(order).Offset(offset).Limit(limit).Find(&info)
	if db.Error != nil {
		return info, db.Error
	}
	return info, nil
}
